import shortuuid

from common.mysql.MySQLhelper import get_sql_helper
from common.mysql.entity.shequn_key_word_find_info import SheQunKeywordFind

sqlhelper = get_sql_helper()


def insert_key_word_find(accept_message_info_id, category_id, category_name, cmsg, key_id, key_word, key_word_house_id,
                         key_word_house_name, today, user_id, user_name, area_id, is_find_self=0):
    seq = str(shortuuid.ShortUUID().random(20))
    match_id = str(shortuuid.ShortUUID().random(20))
    sqlhelper.insert_one(
        "INSERT INTO shequn_keyword_find (id, create_time, user_id, nick_name, to_user_id, to_user_name, from_user_id, from_user_name, content, "
        "key_word, key_word_id, key_word_house_id, key_word_house_name, category_id, category_name, msg_actual_user_id, msg_actual_user_nickname, message_id, state, is_group, seq, is_find_self, area_id) "
        "VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s); ",
        (
            match_id, today, user_id, user_name, cmsg.to_user_id, cmsg.to_user_nickname, cmsg.from_user_id,
            cmsg.from_user_nickname, cmsg.content, key_word, key_id,
            key_word_house_id, key_word_house_name, category_id, category_name, cmsg.actual_user_id,
            cmsg.actual_user_nickname, accept_message_info_id, 0, cmsg.is_group, seq, is_find_self, area_id))


def get_key_word_find(old_user_id):
    results = sqlhelper.fetch_all("select * from shequn_keyword_find where user_id = %s",
                                  (old_user_id))

    key_word_find_list = []

    for result in results:
        key_word_find_info = SheQunKeywordFind(*result)
        key_word_find_list.append(key_word_find_info)
    return key_word_find_list


def update_key_word_list(values):
    sql = 'UPDATE shequn_keyword_find SET from_user_id = %s, to_user_id = %s, user_id = %s WHERE id = %s; '
    sqlhelper.update_list(sql, values)
